package com.xjoyt.pay.master.core.order.handle.callback;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.xjoyt.pay.constants.StringConstant;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.gateway.params.CallbackParam;
import com.xjoyt.pay.master.configs.AppConfig;
import com.xjoyt.pay.master.core.order.handle.AbstractOrderCallbackHandle;
import com.xjoyt.pay.master.dto.CallbackResult;
import com.xjoyt.pay.master.params.NotifyCallbackParam;
import com.xjoyt.pay.master.params.WechatOrderCallbackParam;
import com.xjoyt.pay.master.po.DistributorPreference;
import com.xjoyt.pay.master.po.Order;
import com.xjoyt.pay.master.repository.DistributorPreferenceRepository;
import com.xjoyt.pay.master.repository.OrderRepository;

/**
 * 通知处理
 * 
 * @author Administrator
 *
 */
@Component
public class NotifyHandle extends AbstractOrderCallbackHandle {

	@Autowired
	private AppConfig appConfig;

	@Autowired
	private OrderRepository repository;

	@Autowired
	private DistributorPreferenceRepository distributorPreferenceRepository;

	@Autowired
	private RestTemplate restTemplate;

	@Override
	public ExecuteResult<CallbackResult> handle(CallbackParam param, Order entity) {
		DistributorPreference preference = entity.merchant == null || StringUtils.isBlank(entity.merchant.id) ? null
				: distributorPreferenceRepository.findOne(entity.merchant.id);
		try {
			NotifyCallbackParam p = getNotifyParam(entity, preference);
			String callback = restTemplate.postForObject(appConfig.notifyUrl, p, String.class);
			if (StringConstant.CALLBACK_TARGET.equals(callback)) {
				entity.notify = true;
				entity.notifyDate = new Date();
				repository.save(entity);
				return getNext().handle(param, entity);
			}
			if (entity.notify == null) {
				entity.notify = false;
				repository.save(entity);
			}
			return ExecuteResult.fail("下游回调不正确");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("通知下游出错", e.toString());
			if (entity.notify == null) {
				entity.notify = false;
				repository.save(entity);
			}
			return ExecuteResult.fail("下游回调处理异常");
		}
	}

	private NotifyCallbackParam getNotifyParam(Order entity, DistributorPreference preference) {
		NotifyCallbackParam result = new NotifyCallbackParam();
		result.url = entity.notifyUrl;

		result.param = new WechatOrderCallbackParam();
		result.param.no = entity.no;
		result.param.merchantNo = entity.merchant == null ? null : entity.merchant.no;
		result.param.outTradeNo = entity.outTradeNo;
		result.param.date = entity.doneDate;
		result.param.money = entity.money;

		if (preference != null) {
			result.notifyFormat = preference.notifyFormat;
			result.notifyType = preference.notifyType;
			result.signType = preference.signType;
			result.token = preference.token;
		}

		// if (preference == null || preference.notifyType == null ||
		// preference.notifyType == 0) {
		// return param;
		// }
		// if (preference.notifyType == 2) {
		// return param;
		// }
		return result;
	}

}
